package jzqb.service.impl;

import haha.life.common.CutPageBean;
import jzqb.bo.CollectClassifyBo;
import jzqb.bo.CollectDataBo;
import jzqb.common.ContainUtils;
import jzqb.common.Result;
import jzqb.dao.CollectClassifyDao;
import jzqb.dao.CollectDataDao;
import jzqb.service.CollectDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 关注公众号：MarkerHub
 * @since 2020-09-22
 */
@Service
public class CollectDataServiceImpl implements CollectDataService {


    @Autowired
    CollectDataDao collectDataDao;
    @Autowired
    CollectClassifyDao collectClassifyDao;

    @Override
    public Result insertCollectData(CollectDataBo collectDataBo) {
        Boolean finaRes = false;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String dateTime = df.format(new Date());
        collectDataBo.setCreateTime(dateTime);
        //1. 获得收藏分类ID  List
        CollectDataBo res = collectDataDao.getCollectDataByCondition(collectDataBo);
        if (res == null) {
            Boolean aBoolean = collectDataDao.insertCollectData(collectDataBo);
            if (aBoolean) {
                return Result.succ("新建收藏数据成功！");
            }else {
                return Result.fail("新建收藏数据失败！");
            }
        }
        // 即使查询到了 也需要进行更新
        Boolean temp =collectDataDao.updateCollectDataInfo(collectDataBo);
        //判断新增的是否在里面
        Boolean falg = ContainUtils.KBContains(res.getCollectClassifyID(), collectDataBo.getCollectClassifyID());
        if (falg) {
            return Result.fail("该数据已收藏！");
        } else {
            //如果不在的话进行添加
            String addClassFy = collectDataBo.getCollectClassifyID();
            String oldClassFy = res.getCollectClassifyID();
            String newClassFy = addClassFy + ";" + oldClassFy;
            //然后进行添加
            res.setCollectClassifyID(newClassFy);
            finaRes = collectDataDao.updateCollectData(res);
        }
        return Result.succ("新建收藏数据成功!");
    }


    @Override
    public Boolean deleteCollectData(CollectDataBo collectDataBo) {
        Boolean finaRes = false;
        // 对collectData进行删除
        CollectDataBo res = collectDataDao.getCollectDataByCondition(collectDataBo);
        Boolean falg = ContainUtils.KBContains(res.getCollectClassifyID(), collectDataBo.getCollectClassifyID());
        // 如果不在里面
        if (falg == false) {
            return false;
        }
        // 如果在里面的话  删除点 ，然后进行更新
        String newString = ContainUtils.KBDelete(res.getCollectClassifyID(), collectDataBo.getCollectClassifyID());
        res.setCollectClassifyID(newString);
        finaRes = collectDataDao.updateCollectData(res);
        return finaRes;
    }

    @Override
    public CutPageBean<CollectDataBo> getCollectDataByClassyId(CollectDataBo collectDataBo) {
        CutPageBean<CollectDataBo> cutPageBean = new CutPageBean<>();
        collectDataBo.setStart((collectDataBo.getPage() - 1) * collectDataBo.getPageSize());
        // 然后这里我们先拿到这个收藏下面的所有数据,然后拼接DataSource 和 id 然后belongTo
        List<CollectDataBo> res = collectDataDao.getCollectDataByClassyId(collectDataBo);
        for (CollectDataBo result : res) {
            List<CollectClassifyBo> collectClassifyBos = new ArrayList<>();
            String[] arr = result.getCollectClassifyID().split(";");
            for (String id : arr) {
                CollectClassifyBo collectClassifyBo = collectClassifyDao.getCollectById(id);
                collectClassifyBos.add(collectClassifyBo);
            }
            result.setCollectList(collectClassifyBos);
        }
        Long count = collectDataDao.getCollectDataByClassyIdCount(collectDataBo);
        cutPageBean.initCutPage(count.intValue(), collectDataBo.getPageSize(), res);
        return cutPageBean;
    }

    @Override
    public CutPageBean<CollectDataBo> getAllCollectionData(CollectDataBo collectDataBo) {
        CutPageBean<CollectDataBo> cutPageBean = new CutPageBean<>();
        collectDataBo.setStart((collectDataBo.getPage() - 1) * collectDataBo.getPageSize());
        List<CollectDataBo> res = collectDataDao.getAllCollectionData(collectDataBo);
        for (CollectDataBo result : res) {
            List<CollectClassifyBo> collectClassifyBos = new ArrayList<>();
            String[] arr = result.getCollectClassifyID().split(";");
            for (String id : arr) {
                CollectClassifyBo collectClassifyBo = collectClassifyDao.getCollectById(id);
                collectClassifyBos.add(collectClassifyBo);
            }
            result.setCollectList(collectClassifyBos);
        }
        Long count = collectDataDao.getAllCollectionDataCount(collectDataBo);
        cutPageBean.initCutPage(count.intValue(), collectDataBo.getPageSize(), res);
        return cutPageBean;
    }

    @Override
    public List<CollectDataBo> getCollectDatasId(CollectClassifyBo collectClassifyBo) {
        return collectDataDao.getCollectDatasId(collectClassifyBo);
    }
}